package cn.myeasyai.util;

import cn.myeasyai.core.face.FaceModel;
import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

public class FileUtil {
    public static File getResourceFile(String fileName) {
        ClassPathResource  classPathResource = new ClassPathResource(fileName);
        return new File(classPathResource.getPath());
    }

    private static String readPaper(File file) {
        InputStream read = null;
        String context = null;
        try {
            read = Files.newInputStream(file.toPath());
            byte[] bt = new byte[read.available()];
            read.read(bt);
            context = new String(bt, StandardCharsets.UTF_8);
            read.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (read != null) {
                try {
                    read.close(); //确保关闭
                } catch (IOException el) {

                }
            }
        }
        return context;
    }

    public static FaceModel readCreatorModel2(String path) {
        String a = readPaper(new File(path));
        return JSONObject.parseObject(a, FaceModel.class);
    }





}
